<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ include file="/include/taglibs.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<head>
    <title>流程实例列表</title>
    <link rel="stylesheet" href="${ctx}/assets/admin/LingUI/css/ui.jqgrid.css" />
</head>

<div class="row">
    <div class="col-xs-12">	    <%--查询条件更多 --%>
        <table id="wfins_list_table" class="grid-table"></table> 	<%--jqgrid数据表格 --%>
        <div id="wfins_list_pager"></div>							<%--jqgrid表格 页脚 --%>
    </div>
</div>
<script type="text/javascript">
    var scripts = [null,
        "${ctx}/assets/admin/LingUI/js/jqGrid/jquery.jqGrid.js",
        "${ctx}/assets/admin/LingUI/js/jqGrid/i18n/grid.locale-cn.js",
        "${ctx}/assets/admin/LingUI/js/control.js",
        "${ctx}/assets/admin/LingUI/js/jsutil/js/jquery.filterSearch.js",   //筛选搜索
        null];
    var _grid_id = "#wfins_list_table";	//表格 选择器
    var _pager_id = "#wfins_list_pager";	//表格页脚 选择器
    ace.load_ajax_scripts(scripts, function() {

        //调整以适应页面大小  其中 $(".page-content") 为jqgrid的父级容器
        $(window).off('resize.jqGrid').on('resize.jqGrid', function () {
            setTimeout(function(){
                $(_grid_id).jqGrid("setGridWidth",$(".page-content").width());
                $(".ui-jqgrid-bdiv").width($(".page-content").width()+1);
            },50);
        });

        jQuery(_grid_id).jqGrid({
            mtype : "POST",
            url: "${ctx}/${frameworkPath}/wf/instance/list",
            colNames:['编号','流程key','开始时间','启动人','当前操作人','当前操作','操作'],
            colModel:[
                {name:'id',index:'id', width:0,  editable: true,  hidden:true},
                {name:'key',index:'key', width:60,  editable: true},
                {name:'startTime',index:'startTime', width:60,  editable: true,align:'center'},
                {name:'startUserId',index:'startUserId', width:30,  editable: true,align:'center'},
                {name:'lastOperator',index:'lastOperator', width:30,  editable: true,align:'center'},
                {name:'lastOperation',index:'lastOperation', width:50,  editable: true,align:'center'},
                {name:'action',index:'', width:100, fixed:false, sortable:false,align:'center', formatter:
                        function(cellvalue, options, rowObject) {
                            var s='<div class=" action-buttons">'
                            s=s+  '<a class="blue my-tooltip-link tooltip-primary" title="编辑" style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 " onclick="editrow(\''+rowObject.id+'\')"></i></a>'    ;
                            s=s+  '<a class="red my-tooltip-link tooltip-primary" title="删除" style="cursor:pointer"><i class="ace-icon fa fa-trash-o bigger-130"  onclick="deleteInstance(\''+rowObject.deploymentId+'\')"></i></a>' ;
                            return s+'</div>';}
                }
            ],
            pager : _pager_id, //分页信息 放置的位置
            page : lui.pageNum(_grid_id)
        });
        //自定义工具栏 =============BEGIN======================//
        jQuery(_grid_id)
                .jqGrid('navGrid',_pager_id)
                .navButtonAdd( _pager_id,{
                    title : "上传流程",//按钮悬浮提示
                    buttonicon : "ace-icon fa fa-upload purple",
                    onClickButton: function(){
                        lui.open({
                            title: '新建流程',
                            content: "${ctx}/${frameworkPath}/wf/definition/create",
                            area: ['700px', '400px'],
                            yes: function(index, layero){
                                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                                var retObj = iframeWin.formValid();
                                if(retObj.state){
                                    lui.info(retObj.text);
                                    toLocation('${ctx}/service/editor?id='+retObj.id);
                                }
                            }
                        });
                    }
                })
                .navButtonAdd( _pager_id,{
                    title : "查看流程",
                    buttonicon : "ace-icon fa fa-eye blue",
                    onClickButton :  viewDef
                })
                .navButtonAdd( _pager_id,{
                    title : "删除",
                    buttonicon : "ace-icon fa fa-trash-o red",
                    onClickButton : deleteRows
                })
                .navButtonAdd( _pager_id,{
                    title : "刷新",
                    buttonicon : "ace-icon fa fa-refresh orange",
                    onClickButton : function(){
                        //callback();
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                })
    });

    //编辑数据
    function editrow(id){
        lui.open({
            title: '编辑模块',
            content: "${ctx}/${frameworkPath}/module/edit?id="+id,
            area: ['600px', '300px'],
            yes: function(index, layero){
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                var retObj = iframeWin.formValid();
                if(retObj.text){
                    lui.info(retObj.text);
                    if(retObj.state){
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
            }
        });
    }

    function viewDef(){
        var ids =  $("#wfdef_list_table").jqGrid('getGridParam','selarrrow');
        if(ids.length<1){
            lui.warn('至少选择一行数据');
            return;
        }
        else if(ids.length>1){
            lui.warn("最多选择一行数据");
            return;
        }
        lui.open({
            title: '查看流程',
            content: "${ctx}/${frameworkPath}/workflow/resource/process-definition?type=image&pid="+ids[0],
            area: ['700px', '400px'],
            yes: function(index, layero){
                lui.close(index);
            }
        });
    }

    //删除
    function deleteInstance(id){
        lui.confirm('确定要删除该记录吗',
                function(){
                    var idArray = new Array();
                    idArray.push(id);
                    $.ajax({
                        type:'POST',
                        url:'${ctx}/${frameworkPath}/wf/definition/delete',
                        traditional:true,
                        data:{'deployIds':idArray},
                        success:function(data){
                            if(data.success){
                                lui.success("删除成功");
                                jQuery(_grid_id).trigger("reloadGrid");
                            }
                            else{
                                lui.fail("删除失败");
                            }
                        },
                        error: function(XmlHttpRequest, textStatus, errorThrown){
                            lui.fail("删除失败");
                        }
                    });
                },
                function(){});
    }

    function deleteRows(){
        var ids =  $("#wfdef_list_table").jqGrid('getGridParam','selarrrow');
        if(ids.length<1){
            lui.warn('至少选择一行数据');
            return;
        }
        lui.confirm('确定要删除该记录吗',
                function(){
                    var idArray = new Array();
                    for(var i=0; i<ids.length; i++){
                        idArray.push(ids[i]);
                    }
                    $.ajax({
                        type:'POST',
                        url:'${ctx}/${frameworkPath}/wf/definition/delete',
                        traditional:true,
                        data:{'deployIds':idArray},
                        success:function(data){
                            if(data.success){
                                lui.success("删除成功");
                                jQuery(_grid_id).trigger("reloadGrid");
                            }
                            else{
                                lui.fail("删除失败");
                            }
                        },
                        error: function(XmlHttpRequest, textStatus, errorThrown){
                            lui.fail("删除失败");
                        }
                    });
                },
                function(){}
        );
    }
</script>